<!--
* @author   小马
* @time     2024-10-31 13:27:01  星期四
* @description     搜索组件  
* 组件使用参数需求：
* 1. searchData: 父组件传值，搜索条件数据
* 2. searchFetch: 子传父事件，搜索条件提交
* 3. ruleForm: 表单数据
* 4. visiable: 高级查询按钮是否显示
* 5. searchFun: 查询按钮点击事件
* 6. resetForm: 重置按钮点击事件
* 7. searchData: 父组件传值，搜索条件数据 类型为数组，默认值为空数组 可以根据需要自行修改
-->
<template>
  <div class="search-container">
    <el-form ref="ruleFormRef" :model="ruleForm" label-width="80px" class="demo-ruleForm">
      <el-row :gutter="20">
        <!-- 渲染前三个项目 -->
        <el-col :span="6" v-for="(item, index) in searchData.slice(0, 3)" :key="index">
          <div class="grid-content ep-bg-purple">
            <el-form-item :label="item.label">
              <el-input v-model="ruleForm[item.module]" v-if="item.type === 'input'" />
              <el-select v-if="item.type === 'select'" v-model="ruleForm[item.module]">
                <el-option
                  v-for="items in item.options"
                  :key="items.value"
                  :label="items.label"
                  :value="items.value"
                >
                  <div
                    v-if="item.visible"
                    style="display: flex; justify-content: space-between; align-items: center;"
                  >
                    <span>{{ items.label }}</span>
                    <span>{{ items.value }}</span>
                  </div>
                </el-option>
              </el-select>
              <el-date-picker
                v-model="ruleForm[item.module]"
                :format="item.format"
                :value-format="item.format"
                type="date"
                v-if="item.type === 'date'"
              ></el-date-picker>
            </el-form-item>
          </div>
        </el-col>
        <!-- 固定操作按钮 -->
        <el-col :span="6">
          <el-form-item>
            <el-button v-if="searchData.length > 3" type="success" @click="visiable =!visiable">高级查询</el-button>
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          </el-form-item>
        </el-col>
        <!-- 渲染剩余的项目 -->
        <el-col
          :span="6"
          v-for="(item, index) in searchData.slice(3)"
          :key="index + 3"
          v-if="visiable"
        >
          <div class="grid-content ep-bg-purple">
            <el-form-item :label="item.label" :prop="item.module">
              <el-input v-model="ruleForm[item.module]" v-if="item.type === 'input'" />
              <el-select v-model="ruleForm[item.module]" v-if="item.type === 'select'">
                <el-option
                  v-for="items in item.options"
                  :key="items.value"
                  :label="items.label"
                  :value="items.value"
                >
                  <div
                    v-if="item.visible"
                    style="display: flex; justify-content: space-between; align-items: center;"
                  >
                    <span>{{ items.label }}</span>
                    <span>{{ items.value }}</span>
                  </div>
                </el-option>
              </el-select>
              <el-date-picker
                v-model="ruleForm[item.module]"
                type="date"
                v-if="item.type === 'date'"
              ></el-date-picker>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
const visiable = ref(false);
//接受父组件传值
const props = defineProps({
  //父组件传值
  searchData: {
    type: Array,
    default: () => []
  }
});
// 表单数据
const ruleForm = ref({});
//子传父事件
const emit = defineEmits(["searchFetch", "resetFetch"]);
const searchFun = () => {
  emit("searchFetch", ruleForm.value);
};
const resetForm = formRef => {
  ruleForm.value = {};
  let pageOptions = {
    pageNum: 1,
    pageSize: 10
  };
  emit("resetFetch", pageOptions);
};
</script>

<style lang="scss" scoped>
.search-container {
  width: 100%;
  min-width: 100%;
  box-sizing: border-box;
  padding: 20px 0;

  :deep(.el-form) {
    width: 100%;
    .el-input,
    .el-select,
    .el-date-picker {
      width: 100%;
    }
    .el-form-item {
      margin-bottom: 18px;
    }
    .el-form-item__label {
      width: 80px !important;
      text-align: right;
    }
  }
  :deep(.el-form-item__content) {
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
  :deep(.el-row) {
    width: 100%;
    flex-wrap: wrap;
    @media (max-width: 1200px) {
      flex-direction: column;
      .el-col {
        max-width: 100% !important;
      }
      :deep(.el-col-6) {
        max-width: 100% !important;
      }
    }
    @media (max-width: 768px) {
      flex-direction: column;

      .el-col {
        max-width: 100% !important;
            margin-bottom: 10px;
      }
      :deep(.el-col-6) {
        max-width: 100% !important;
        margin-bottom: 10px;
      }
    }
  }
  :deep(.el-col) {
    box-sizing: border-box;
  }
}
</style> 